from gm.api import *
import numpy as np
import pandas as pd
from sklearn import preprocessing

#这段代码是获取MSCI成分股，返回一个list
def read_MSCI():
    symbol_list = []
    f2 = open("../MSCI_tools/MSCI成分股.txt", "r")
    lines = f2.readlines()
    for line3 in lines:
        symbol_list.append(line3.strip())
    return symbol_list



#这段代码是获取返回的data数据中的对应因子的list
def get_data_value(data,factor):
    result = []
    for _ in data:
        result.append(_[factor])
    return result


def get_symbol_list(index,now):


    symbol_list = get_history_constituents(index=index, start_date=now)[0].get("constituents").keys()
    symbol_list_not_suspended = get_history_instruments(symbols=symbol_list, start_date=now, end_date=now)
    symbol_list = [item['symbol'] for item in symbol_list_not_suspended if not item['is_suspended']]

    _symbol_list = symbol_list
    symbol_list = []
    for _ in _symbol_list:
        symbol_list.append(_)
    return symbol_list


def from_df_get_df_score(df, weight, start, end=0):
    """
    归一化是在列的方向对行数据进行操作：比如调用sklearn.preprocessing里的MinMaxscaler；
    标准化是在行的方向对列数据进行操作：比如调用sklearn.preprocessing里的Normalizer；
    因为是对 每行 求和，这里建议对建立的矩阵使用MinMaxscaler

    """
    if end == 0:
        df_factor = df.iloc[:, start:]
    else:
        df_factor = df.iloc[:, start:end]
    df_factor = np.asmatrix(df_factor)
    # 先进行列归一化，然后在对每行进行标准化处理
    df_factor = preprocessing.MinMaxScaler().fit_transform(df_factor)
    df_factor = preprocessing.Normalizer().fit_transform(df_factor)
    _weight = []
    for _ in weight:
        _weight.append([_])
    weight_mat = np.asmatrix(_weight)  # 这里设置的beta值偏低，选择不太活跃的股
    res = np.dot(df_factor, weight_mat)
    df["score"] = (res)
    return df


def from_df_get_symbol_list_by_score(df,weight,start,end=0,ascending=True):
    """
    归一化是在列的方向对行数据进行操作：比如调用sklearn.preprocessing里的MinMaxscaler；
    标准化是在行的方向对列数据进行操作：比如调用sklearn.preprocessing里的Normalizer；
    因为是对 每行 求和，这里建议对建立的矩阵使用MinMaxscaler

    """
    if end == 0:
        df_factor = df.iloc[:, start:]
    else:
        df_factor = df.iloc[:, start:end]

    df_factor = np.asmatrix(df_factor)

    #先进行列归一化，然后在对每行进行标准化处理
    df_factor = preprocessing.MinMaxScaler().fit_transform(df_factor)
    df_factor = preprocessing.Normalizer().fit_transform(df_factor)

    _weight = []
    for _ in weight:
        _weight.append([_])

    weight_mat = np.asmatrix(_weight)

    res = np.dot(df_factor, weight_mat)

    df["score"] = (res)
    df.index = df.score
    df = df.sort_index(ascending=ascending)  #这里是将最后的趋势有小到大排列

    _symbol_list = df["symbol"].values
    symbol_list = []
    for _ in _symbol_list:
        symbol_list.append(_)

    return symbol_list